/*
 Copyright 2012 Sven-Ove Bjerkan

 This file is part of KO-Ozi.

 KO-Ozi is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 KO-Ozi is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with KO-Ozi. If not, see <http://www.gnu.org/licenses/>.
 */
package com.koozi;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * This class handles screenshots of the map
 * @author Sven-Ove Bjerkan
 */
public class Screenshot {

    private String folderpath;

    public Screenshot() {
        Settings settings = Settings.getInstance();
        this.folderpath = settings.getProjectPath() + "/Skjermdumper/";
    }

    /**
     * Multi-monitor support
     * @return number of monitors
     */
    public static int getNumScreens() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            GraphicsDevice[] gs = ge.getScreenDevices();

            // Get number of screens
            int numScreens = gs.length;
            return numScreens;
        } catch (HeadlessException e) {
            // Is thrown if there are no screen devices
        }
        return -1;
    }
    
    /**
     * Take the screenshot
     * @param screenNo if multiple monitors, specify which one to use
     * @param filename filename to save to
     * @return true on success
     */
    public boolean takeScreenshot(int screenNo, String filename) {
        File f = new File(folderpath);
        // Check if directory exists, create otherwise
        if (!f.isDirectory() && !f.mkdir()) {
            return false;
        }

        // Create the screenshot
        try {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
            GraphicsDevice[] screens = ge.getScreenDevices(); 
            
            Robot robot = new Robot(screens[screenNo-1]);
            Rectangle screenRectangle = screens[screenNo-1].getDefaultConfiguration().getBounds();
            screenRectangle.x = 0;
            screenRectangle.y = 0;
            
            BufferedImage image = robot.createScreenCapture(screenRectangle);

            // Some calculations to ignore the toolbars of the program
            int height = (int) (screenRectangle.height * 0.86);
            BufferedImage dest = image.getSubimage(0, (int) (screenRectangle.height * 0.135), 
                    screenRectangle.width, height);

            return ImageIO.write(dest, "png", new File(folderpath + filename));
        } catch (Exception ex) {
            Logger.getLogger(Screenshot.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public static void showScreenshot(String selFile) {
        JFrame editorFrame = new JFrame(selFile);
        selFile = Settings.getInstance().getProjectPath() + "/Skjermdumper/" + selFile;

        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(selFile));
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        ImageIcon imageIcon = new ImageIcon(image);
        JLabel jLabel = new JLabel();
        jLabel.setIcon(imageIcon);
        editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

        editorFrame.pack();
        editorFrame.setLocationRelativeTo(null);
        editorFrame.setVisible(true);
    }
}